package com.example.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.*;
import com.example.pojo.entity.Inventory;
import com.example.pojo.entity.Material;
import com.example.pojo.entity.Warehouse;
import com.example.service.IInventoryService;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 库存表 服务实现类
 * </p>
 *
 * @author wy
 * @since 2024-07-17
 */
@Service
public class InventoryServiceImpl extends ServiceImpl<InventoryMapper, Inventory> implements IInventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public void add(Inventory inventory) {
        inventory.setIsDeleted(0);
        inventory.setVersion(0);
        inventoryMapper.insert(inventory);
    }

    @Override
    public Page<Inventory> selectPage(Inventory inventory, Integer pageNum, Integer pageSize) {
        MPJLambdaWrapper<Inventory> mpjLambdaWrapper = new MPJLambdaWrapper<>();
        mpjLambdaWrapper
                .selectAll(Inventory.class)
                .selectAs(Material::getName, Inventory::getMaterialName)
                .selectAs(Warehouse::getName, Inventory::getWarehouseName)
                .innerJoin(Material.class,Material::getId,Inventory::getMaterialId)
                .innerJoin(Warehouse.class,Warehouse::getId,Inventory::getWarehouseId)
                .eq(Inventory::getIsDeleted,0)
                .eq(ObjectUtil.isNotNull(inventory.getMaterialId()), Inventory::getMaterialId, inventory.getMaterialId())
                .eq(ObjectUtil.isNotNull(inventory.getWarehouseId()), Inventory::getWarehouseId, inventory.getWarehouseId());
        return page(new Page<>(pageNum, pageSize), mpjLambdaWrapper);
    }

}
